//
//  UIImage+Size.m
//  QuTaoTao
//
//  Created by weijhon on 2018/1/19.
//  Copyright © 2018年 weijhon. All rights reserved.
//

#import "UIImage+Size.h"

@implementation UIImage (Size)

+ (UIImage *) resizeImage:(UIImage *) image withNewSize:(CGSize) newSize{
    
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

+ (CGSize) scaleImage:(UIImage *) image withLength:(CGFloat) imageLength{
    
    CGFloat newWidth = 0.0f;
    CGFloat newHeight = 0.0f;
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    
    if (width > imageLength || height > imageLength){
        
        if (width > height) {
            
            newWidth = imageLength;
            newHeight = newWidth * height / width;
            
        }else if(height > width){
            
            newHeight = imageLength;
            newWidth = newHeight * width / height;
            
        }else{
            
            newWidth = imageLength;
            newHeight = imageLength;
        }
        
    }else{
        return CGSizeMake(width, height);
    }
    
    return CGSizeMake(newWidth, newHeight);
}

+ (CGSize) scaleImage:(UIImage *)image withWitdh:(CGFloat)imageWidth {
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    
    CGFloat newWidth = imageWidth;
    CGFloat newHeight = 0.0f;
    if (width == imageWidth) {
        return CGSizeMake(width, height);
    }else {
        newHeight = newWidth * height / width;
    }
    return CGSizeMake(newWidth, newHeight);
}

+ (CGSize) scaleImage:(UIImage *)image withHeight:(CGFloat)imageHeight {
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    
    CGFloat newWidth = 0.0f;
    CGFloat newHeight = imageHeight;
    if (height == imageHeight) {
        return CGSizeMake(width, height);
    }else {
        newWidth = newHeight*width/height;
    }
    return CGSizeMake(newWidth, newHeight);
}

@end
